<?php
declare (strict_types = 1);

namespace plugin\ky_black\app\model;

use think\Model;

class BusinessPartner extends Model
{
    protected $name = 'business_partner';
    
    protected $schema = [
        'id'                => 'int',
        'main_id'          => 'int',
        'business_type'    => 'string',
        'name'             => 'string',
        'address'          => 'string',
        'contact_phone'    => 'string',
        'service_level'    => 'string',
        'cooperation_status' => 'int',
        'area_desc'        => 'string',
        'service_items'    => 'string',
        'service_content'  => 'string',
        'created_at'       => 'datetime',
        'deposit_amount'   => 'float',
        'is_open_direct'   => 'int'
    ];

    // 商家类型
    const TYPE_SCENIC = 'scenic';      // 景区
    const TYPE_HOTEL = 'hotel';        // 酒店
    const TYPE_RESTAURANT = 'restaurant'; // 餐饮
    const TYPE_SHOPPING = 'shopping';   // 购物
    
    // 服务等级
    const LEVEL_VIP = 'vip';           // VIP商家
    const LEVEL_NORMAL = 'normal';     // 普通商家
    
    // 合作状态
    const STATUS_DISABLED = 0;         // 停用
    const STATUS_NORMAL = 1;           // 正常

    /**
     * 获取商家类型列表
     */
    public static function getTypeList(): array
    {
        return [
            self::TYPE_SCENIC => '景区',
            self::TYPE_HOTEL => '酒店',
            self::TYPE_RESTAURANT => '餐饮',
            self::TYPE_SHOPPING => '购物'
        ];
    }

    /**
     * 获取服务等级列表
     */
    public static function getLevelList(): array
    {
        return [
            self::LEVEL_VIP => 'VIP商家',
            self::LEVEL_NORMAL => '普通商家'
        ];
    }

    /**
     * 获取状态列表
     */
    public static function getStatusList(): array
    {
        return [
            self::STATUS_NORMAL => '正常',
            self::STATUS_DISABLED => '停用'
        ];
    }

    /**
     * 商家类型获取器
     */
    public function getBusinessTypeTextAttr($value, $data)
    {
        return self::getTypeList()[$data['business_type']] ?? $data['business_type'];
    }

    /**
     * 服务等级获取器
     */
    public function getServiceLevelTextAttr($value, $data)
    {
        return self::getLevelList()[$data['service_level']] ?? $data['service_level'];
    }

    /**
     * 合作状态获取器
     */
    public function getCooperationStatusTextAttr($value, $data)
    {
        return self::getStatusList()[$data['cooperation_status']] ?? '未知';
    }

    /**
     * 搜索器
     */
    public function scopeSearch($query, $params)
    {
        // 商家名称搜索
        if (!empty($params['keyword'])) {
            $query->where('name|contact_phone', 'like', "%{$params['keyword']}%");
        }
        
        // 商家类型筛选
        if (!empty($params['business_type'])) {
            $query->where('business_type', $params['business_type']);
        }
        
        // 合作状态筛选
        if (isset($params['cooperation_status'])) {
            $query->where('cooperation_status', $params['cooperation_status']);
        }
        
        // 服务等级筛选
        if (!empty($params['service_level'])) {
            $query->where('service_level', $params['service_level']);
        }

        return $query;
    }
} 